﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace block_explorer.Util
{
    class BoundingBoxEx
    {
        public static Vector3 Dimensions(BoundingBox a)
        {
            return a.Max - a.Min;
        }

        public static Vector3 Center(BoundingBox a)
        {
            return a.Min + Dimensions(a) * 0.5f;
        }

        public static BoundingBox Union(BoundingBox a, BoundingBox b)
        {
            BoundingBox c = new BoundingBox();
            c.Min.X = Math.Max(a.Min.X, b.Min.X);
            c.Max.X = Math.Min(a.Max.X, b.Max.X);
            c.Min.Y = Math.Max(a.Min.Y, b.Min.Y);
            c.Max.Y = Math.Min(a.Max.Y, b.Max.Y);
            c.Min.Z = Math.Max(a.Min.Z, b.Min.Z);
            c.Max.Z = Math.Min(a.Max.Z, b.Max.Z);
            return c;
        }

        // build a box that contains both given boxes
        public static BoundingBox Contain(BoundingBox a, BoundingBox b)
        {
            Vector3 min = new Vector3((float)Math.Min(a.Min.X, b.Min.X), (float)Math.Min(a.Min.Y, b.Min.Y), (float)Math.Min(a.Min.Z, b.Min.Z));
            Vector3 max = new Vector3((float)Math.Max(a.Max.X, b.Max.X), (float)Math.Max(a.Max.Y, b.Max.Y), (float)Math.Max(a.Max.Z, b.Max.Z));
            return new BoundingBox(min, max);
        }

        public static VertexBuffer CreateWireframe(GraphicsDevice device, BoundingBox a, Color color)
        {
            VertexBuffer buffer = new VertexBuffer(device, VertexPositionColor.VertexDeclaration, 24, BufferUsage.WriteOnly);
            VertexPositionColor[] verts = new VertexPositionColor[24];

            verts[0] = new VertexPositionColor(new Vector3(a.Min.X, a.Min.Y, a.Min.Z), color);
            verts[1] = new VertexPositionColor(new Vector3(a.Min.X, a.Min.Y, a.Max.Z), color);

            verts[2] = new VertexPositionColor(new Vector3(a.Min.X, a.Min.Y, a.Max.Z), color);
            verts[3] = new VertexPositionColor(new Vector3(a.Max.X, a.Min.Y, a.Max.Z), color);

            verts[4] = new VertexPositionColor(new Vector3(a.Max.X, a.Min.Y, a.Max.Z), color);
            verts[5] = new VertexPositionColor(new Vector3(a.Max.X, a.Min.Y, a.Min.Z), color);

            verts[6] = new VertexPositionColor(new Vector3(a.Max.X, a.Min.Y, a.Min.Z), color);
            verts[7] = new VertexPositionColor(new Vector3(a.Min.X, a.Min.Y, a.Min.Z), color);

            verts[8] = new VertexPositionColor(new Vector3(a.Min.X, a.Max.Y, a.Min.Z), color);
            verts[9] = new VertexPositionColor(new Vector3(a.Min.X, a.Max.Y, a.Max.Z), color);

            verts[10] = new VertexPositionColor(new Vector3(a.Min.X, a.Max.Y, a.Max.Z), color);
            verts[11] = new VertexPositionColor(new Vector3(a.Max.X, a.Max.Y, a.Max.Z), color);

            verts[12] = new VertexPositionColor(new Vector3(a.Max.X, a.Max.Y, a.Max.Z), color);
            verts[13] = new VertexPositionColor(new Vector3(a.Max.X, a.Max.Y, a.Min.Z), color);

            verts[14] = new VertexPositionColor(new Vector3(a.Max.X, a.Max.Y, a.Min.Z), color);
            verts[15] = new VertexPositionColor(new Vector3(a.Min.X, a.Max.Y, a.Min.Z), color);

            verts[16] = new VertexPositionColor(new Vector3(a.Min.X, a.Min.Y, a.Min.Z), color);
            verts[17] = new VertexPositionColor(new Vector3(a.Min.X, a.Max.Y, a.Min.Z), color);

            verts[18] = new VertexPositionColor(new Vector3(a.Min.X, a.Min.Y, a.Max.Z), color);
            verts[19] = new VertexPositionColor(new Vector3(a.Min.X, a.Max.Y, a.Max.Z), color);

            verts[20] = new VertexPositionColor(new Vector3(a.Max.X, a.Min.Y, a.Max.Z), color);
            verts[21] = new VertexPositionColor(new Vector3(a.Max.X, a.Max.Y, a.Max.Z), color);

            verts[22] = new VertexPositionColor(new Vector3(a.Max.X, a.Min.Y, a.Min.Z), color);
            verts[23] = new VertexPositionColor(new Vector3(a.Max.X, a.Max.Y, a.Min.Z), color);

            buffer.SetData(verts);

            return buffer;
        }
    }
}
